package com.wyzz.hr.action;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.axis.utils.StringUtils;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.wf.overtimerequest.OrWfApproverAction;
import com.wyzz.hr.bo.ILeavetypeBO;
import com.wyzz.hr.bo.IOvertimerequestBo;
import com.wyzz.hr.bo.IOvertimetypeBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.pojo.Overtimerequest;
import com.wyzz.hr.pojo.Overtimetype;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.vo.ExaminSearchBean;

public class MyOvertimeSearchAction extends OrWfApproverAction {

	public MyOvertimeSearchAction() {
	}

	public String execute() throws Exception {
		initOTLists();
		if (page == null) {
			page = new Pager();
			page.setOrder("");
		} else if (page.getOrder().trim().length() == 0)
			page.setOrder("");
		if (es_Bean == null)
			es_Bean = new ExaminSearchBean();
		IOvertimerequestBo or_Bo = (IOvertimerequestBo) getBean("overtimerequestBO");
		or_Bo.ExaminOwnSearch(es_Bean, page, getCurrentEmpNo());
		if (getRequest().getAttribute("msg") != null)
			addSuccessInfo((String) getRequest().getAttribute("msg"));
		if (!StringUtils.isEmpty(infoMeg))
			addSuccessInfo(infoMeg);
		if (!StringUtils.isEmpty(errorMsg))
			addErrorInfo(errorMsg);
		Calendar cal = Calendar.getInstance();
		currEmpName = getCurrentEmp().getEmpName();
		currYear = String.valueOf(cal.get(1));
		IOvertimetypeBo otBo = (IOvertimetypeBo) getBean("overtimetypeBO");
		otTypeList = otBo.FindAllOtType();
		return "success";
	}

	public Map getOverTimeData(String empId, String actionURL) {
		Map result = new HashMap();
		if (!StringUtils.isEmpty(actionURL)
				&& !"HR".equals(DWRUtil.checkAuth(actionURL, "execute"))) {
			result.put("error",
					"\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF01");
			return result;
		}
		if (StringUtils.isEmpty(empId))
			empId = getCurrentEmpNo();
		System.out.println(empId);
		IOvertimerequestBo or_Bo = (IOvertimerequestBo) getBean("overtimerequestBO");
		overtimeTotal = or_Bo.getOvertimeTotal(empId);
		if (overtimeTotal == null) {
			result
					.put(
							"error",
							"\u5F53\u524D\u5458\u5DE5\u672C\u5E74\u5EA6\u6CA1\u6709\u52A0\u73ED\u6570\u636E\uFF01");
			return result;
		} else {
			caclData(overtimeTotal);
			result.put("param_succ", newOvertimeTotal);
			System.out.println(result);
			return result;
		}
	}

	private void caclData(String overtimeTotal[][]) {
		if (overtimeTotal == null)
			return;
		newOvertimeTotal = new String[overtimeTotal[13].length - 1][4];
		for (int i = 0; i < newOvertimeTotal.length; i++) {
			for (int j = 0; j < newOvertimeTotal[i].length; j++)
				newOvertimeTotal[i][j] = "0.0";

		}

		int currMonth = DateUtil.getMonth(new Date()) - 1;
		for (int i = 0; i < overtimeTotal[13].length - 1; i++)
			newOvertimeTotal[i][0] = overtimeTotal[13][i];

		for (int i = 0; i < overtimeTotal[currMonth].length - 1; i++)
			newOvertimeTotal[i][1] = overtimeTotal[currMonth][i];

		for (int i = 0; i < overtimeTotal[12].length - 1; i++) {
			newOvertimeTotal[i][2] = overtimeTotal[12][i];
			if (overtimeTotal[12][i].indexOf("(") != -1) {
				String tempNumber = overtimeTotal[12][i].substring(
						overtimeTotal[12][i].indexOf("("), overtimeTotal[12][i]
								.indexOf(")"));
				newOvertimeTotal[i][3] = tempNumber.substring(tempNumber
						.indexOf("(") + 1, tempNumber.indexOf(".") + 2);
			}
		}

		IOvertimetypeBo otBo = (IOvertimetypeBo) getBean("overtimetypeBO");
		List orderAscTypeList = otBo.FindAllOtType();
		String newSortAsc[][] = new String[overtimeTotal[13].length - 1][4];
		for (int j = 0; j < orderAscTypeList.size(); j++) {
			for (int i = 0; i < newOvertimeTotal.length; i++)
				if (((Overtimetype) orderAscTypeList.get(j)).getOtName()
						.equals(newOvertimeTotal[i][0]))
					newSortAsc[j] = newOvertimeTotal[i];

		}

		newOvertimeTotal = newSortAsc;
	}

	public void validate() {
		validateDate(es_Bean);
		validateOTStatus(es_Bean);
	}

	public String get24Point(Date date) {
		if (date == null)
			date = new Date();
		String result = null;
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		if ("00:00".equals(format.format(date)))
			result = "24:00";
		else
			result = format.format(date);
		return result;
	}

	public String delete(String orId, HttpSession session) {
		String result = "suc";
		IOvertimerequestBo overtimerequestBo = (IOvertimerequestBo) getBean("overtimerequestBO");
		Overtimerequest or = overtimerequestBo.loadOvertimerequest(orId);
		if (or == null || or.getOrStatus().intValue() != 21
				|| !getCurrentEmp().equals(or.getOrEmpNo())
				&& !getCurrentEmp().equals(or.getOrCreateBy()))
			return "\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF01";
		Employee currentEmployee = ((Userinfo) session.getAttribute("userinfo"))
				.getEmployee();
		List errors = overtimerequestBo.deleteOvertimereuqest(orId,
				currentEmployee);
		if (errors.size() >= 1)
			result = "\u5220\u9664\u52A0\u73ED\u7533\u8BF7\u5931\u8D25\uFF01";
		return result;
	}

	public Map getTiaoxiuTime(String empId, String actionURL) {
		Map result = new HashMap();
		if (!StringUtils.isEmpty(actionURL)
				&& !"HR".equals(DWRUtil.checkAuth(actionURL, "execute"))) {
			result.put("error",
					"\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF01");
			return result;
		}
		int statusSet_approved[] = { 15, 16 };
		ILeavetypeBO lt_BO = (ILeavetypeBO) getBean("leavetypeBO");
		Leavetype lt = lt_BO.getLeavetypeByCat(Integer.valueOf(11));
		Date now = new Date();
		if (empId == null)
			empId = getCurrentEmpNo();
		Employee emp = new Employee(empId);
		IOvertimerequestBo or_Bo = (IOvertimerequestBo) getBean("overtimerequestBO");
		List tiaoxiuOrList = or_Bo.getEmpTiaoxiuOrList(emp, statusSet_approved);
		if (tiaoxiuOrList.isEmpty() || tiaoxiuOrList == null) {
			result
					.put(
							"error",
							"\u5BF9\u4E0D\u8D77\uFF0C\u5F53\u524D\u5458\u5DE5\u6CA1\u6709\u8C03\u4F11\u52A0\u73ED\u8BB0\u5F55\uFF01");
			return result;
		} else {
			result.put("param_succ", or_Bo
					.getEmpTiaoxiu(lt, now, tiaoxiuOrList));
			return result;
		}
	}

	public ExaminSearchBean getEs_Bean() {
		return es_Bean;
	}

	public void setEs_Bean(ExaminSearchBean es_Bean) {
		this.es_Bean = es_Bean;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String[][] getOvertimeTotal() {
		return overtimeTotal;
	}

	public void setOvertimeTotal(String overtimeTotal[][]) {
		this.overtimeTotal = overtimeTotal;
	}

	public String[][] getNewOvertimeTotal() {
		return newOvertimeTotal;
	}

	public void setNewOvertimeTotal(String newOvertimeTotal[][]) {
		this.newOvertimeTotal = newOvertimeTotal;
	}

	public String getCurrEmpName() {
		return currEmpName;
	}

	public void setCurrEmpName(String currEmpName) {
		this.currEmpName = currEmpName;
	}

	public String getCurrYear() {
		return currYear;
	}

	public void setCurrYear(String currYear) {
		this.currYear = currYear;
	}

	private static final long serialVersionUID = 1L;
	private ExaminSearchBean es_Bean;
	private Pager page;
	private String overtimeTotal[][];
	private String newOvertimeTotal[][];
	private String currEmpName;
	private String currYear;
}
